package com.hxd.mall.module.school.enter

import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.constant.HxdConstantsRouterPath.School.path_school_article
import com.hxd.mall.constant.HxdConstantsRouterPath.School.path_school_audio
import com.hxd.mall.constant.HxdConstantsRouterPath.School.path_school_video
import com.hxd.mall.module.school.bean.Data
import com.hxd.mall.module.school.bean.ItemResource
import com.hxd.mall.module.school.bean.ModuleLayout
import com.hxd.mall.module.school.bean.SchoolBean
import com.hxd.mall.utils.local.*
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.GsonUtil.bean2json
import com.hxd.mall.utils.open.addSetOnClickListener
import com.hxd.mall.utils.open.initGridRecyclerView
import com.hxd.mall.utils.open.initRecyclerView
import kotlinx.android.synthetic.main.fragment_school_layout.*
import org.jetbrains.anko.displayMetrics

/**
 * @author zhaoyong
 * @description: 商学院tab页
 * @date :2019-07-19 18:47
 */
class SchoolFragment : BaseInjectFragment<ISchoolPresenter>(), ISchoolView {

    private var schoolData: Data? = null
    private var articleListAdapter: ArticleListAdapter? = null
    private val audioListAdapter = AudioListAdapter()
    private val videoListAdapter = VideoListAdapter()

    override fun attachLayoutRes(): Int = R.layout.fragment_school_layout

    override fun initViews() {
        smart_refresh_layout.setEnableLoadMore(false)
        smart_refresh_layout.setEnableRefresh(true)

        initListener()
        articleListAdapter = ArticleListAdapter(activity!!.displayMetrics.widthPixels * 8 / 15, (activity!!.displayMetrics?.widthPixels * 8 / 15) * 3 / 4)
        initRecyclerView()
    }

    override fun initData() {
        mPresenter?.getData(false)
    }

    //完成刷新
    override fun finishRefresh() {
        smart_refresh_layout.finishRefresh()
    }

    override fun fetchData(schoolBean: SchoolBean) {
        this.schoolData = schoolBean.data
        schoolData?.run {
            //先设置标题
            tv_articleList_title.text = articleLayout.title
            tv_audioList_title.text = audioLayout.title
            tv_videoList_title.text = videoLayout.title

            reLoadRecyclerView(this)
        }
    }

    private fun refreshList(
        list: List<ItemResource>?,
        itemNum: Int,
        adapter: BaseQuickAdapter<ItemResource, BaseViewHolder>, point: String
    ) {
        if (list == null)
            return
        //每个模块只展示两条
        getListAdapter(if (list.size > itemNum) list.subList(0, itemNum) else list, adapter, point)
    }

    //刷新列表数据 ArticleListAdapter
    private fun initRecyclerView() {
        //新手必看
        articleList.initRecyclerView(
            articleListAdapter!!.apply { refreshList(listOf(), 2, this, "commercial_college_article_niche_click") },
            RecyclerView.HORIZONTAL, LinearLayoutCompat.HORIZONTAL
        )
        //商学院讲堂
        audioList.initRecyclerView(
            audioListAdapter.apply { refreshList(listOf(), 3, this, "commercial_college_music_niche_click") },
            padding = 0, leftPadding = 58, color = R.color.color_F0F0F0
        )

        //销售经验
        videoList.initGridRecyclerView(
            videoListAdapter.apply { refreshList(listOf(), 2, this, "commercial_college_video_niche_click") }, columnSpace = 6
        )
    }

    //刷新列表数据
    private fun reLoadRecyclerView(schoolBean: Data) {
        //新手必看
        val list = schoolBean.articleLayout.resources
        refreshList(list, list.size, articleListAdapter!!, "commercial_college_article_niche_click")

        //商学院讲堂
        refreshList(schoolBean.audioLayout.resources, 3, audioListAdapter, "commercial_college_music_niche_click")

        //销售经验
        refreshList(schoolBean.videoLayout.resources, 2, videoListAdapter, "commercial_college_video_niche_click")
    }

    private fun initListener() {
        smart_refresh_layout.setOnRefreshListener {
            mPresenter!!.getData(true)
        }
        addSetOnClickListener(readMore, listenerMore, lookMore) {
            schoolData?.run {
                when (it) {
                    readMore -> {
                        activity?.sendPoint(more_article_button_click)
                        jump(path_school_article, articleLayout)
                    }
                    listenerMore -> {
                        activity?.sendPoint(more_voice_button_click)
                        jump(path_school_audio, audioLayout)
                    }
                    lookMore -> {
                        activity?.sendPoint(more_vedio_button_click)
                        jump(path_school_video, videoLayout)
                    }
                }
            }
        }
    }

    private fun jump(path: String, bean: ModuleLayout?) {
        routerNavigation(path, mutableMapOf("list" to if (bean == null) "" else bean2json(bean)))
    }

}